package android.explorer;
import java.util.Arrays;
import java.util.Comparator;

import android.app.Activity;
import android.provider.SyncStateContract.Constants;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
public class DirectoryAdapter extends android.widget.BaseAdapter {
	
	final static int SortType_Alpha = 0;
	final static int SortType_Date = 1;
	final static int SortType_FileType = 2;
	final static int SortType_Size = 3;
	final static boolean Ascending = true;
	final static boolean Descending = false;

	
	private LayoutInflater mInflater;
	FileItem[] items;
	int count=0;
	
	
	
	public DirectoryAdapter(Activity context,FileItem [] items){
		mInflater = LayoutInflater.from(context);
		sortData(SortType_Alpha,Ascending,items);
		this.items=items;
		
		count=items.length;
	}
	
	public DirectoryAdapter(int sortType, boolean Ascending, Activity context,FileItem [] items){
		mInflater = LayoutInflater.from(context);
		sortData(sortType,Ascending,items);
		this.items=items;
		
		count=items.length;
	}
	
	
	public int getCount() {
		return count;
	}
	public Object getItem(int index) {
		return items[index];
	}
	public long getItemId(int index) {
		return index;
	}
	public View getView(int position, View convertView, ViewGroup parent) {
		 ViewHolder holder;
		 if (convertView == null) {
			 convertView = mInflater.inflate(R.layout.listview, null);
			 holder = new ViewHolder();
			 holder.icon=(ImageView)convertView.findViewById(R.id.icon);
			 holder.fileName = (TextView) convertView.findViewById(R.id.text01);
			 holder.fileSize=((TextView) convertView.findViewById(R.id.text02));
			 convertView.setTag(holder);
		 } else {
			 holder = (ViewHolder) convertView.getTag();
		 }
		 holder.setFileName(items[position].display);
		 holder.setFilePath(items[position].path);
		 holder.setFileSize(items[position].infor);
		 if(items[position].tag!=null)holder.icon.setImageResource(IconStore.getIconByExtendsion(items[position].tag));
		 else if(items[position].isFile)holder.icon.setImageResource(IconStore.getIconByExtendsion(items[position].path));
		 else holder.icon.setImageResource(R.drawable.folder32);
		 return convertView;
		 
	}
	
	public void sortData(int sortType, final boolean Ascending, FileItem[] items)
	{
		switch (sortType)
		{
			case(SortType_Alpha):
			{
				Log.v("debug", "sort alpha");
				Arrays.sort(items, new Comparator<FileItem>() {
				    public int compare(FileItem entry1, FileItem entry2) {
				      if (Ascending)
				    	  return entry1.display.compareToIgnoreCase(entry2.display);
				      else
				    	  return entry2.display.compareToIgnoreCase(entry1.display);
				    }
				});
				break;
			}
		
			case(SortType_Size):
			{
				Arrays.sort(items, new Comparator<FileItem>() {
				    public int compare(FileItem entry1, FileItem entry2) {
				      if (Ascending)
				    	  return entry1.display.compareTo(entry2.display);
				      else
				    	  return entry2.display.compareTo(entry1.display);
				    }
				});
				break;
			}
	
		}
		
	}
	
}
